package search_simpleHTMLlist;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Searcher {
    //////////////////////////////////////////////////////////////////////
    //
    // New method. Runs through a linked list of words contained within
    // HTML pages and searches for a specific word. Also prints out the
    // URLs of the pages where the given word was found
    //
    // Try searching for the word 'Links', it appears twice in the file
    // under 'http://www.it-c.dk/Internet' and
    // 'http://www.it-c.dk/Internet/sitemap'
    //
    //////////////////////////////////////////////////////////////////////
    
	
	/**
	 * Finds a list of URL's in the HTMLlist that contains the word.
	 * @param l The HTMLlist
	 * @param word The searched word.
	 * @return a String with URLs separated by linebreaks.
	 */
	public static String existsWithURLs(HTMLlist l, String word) {
        // Initialize variables
        String returnValue = "";
        String lastURL = "";
        boolean hasBeenAdded = false;

        // Search through the linked list
        while (l != null) {																/*1*/
            // Look for the "*PAGE:" pattern in the first 6 characters.
            // The substring method will fail if we don't check for words
            // smaller than 6 characters. This creates a bias towards pages
            // than have many words shorter than 6 characters
            if (l.str.length() >= 6 && l.str.substring(0, 6).equals("*PAGE:")) {		/*2*/
                // Yay! We found a URL, discard the identifier, extract
                // the page URL and update the last URL we encountered
                lastURL = l.str.substring(6);

                // Reset the flag to signal that the last URL we encountered
                // has not yet been added to our return string
                hasBeenAdded = false;
            } else if (l.str.equalsIgnoreCase(word)) {									/*3*/
                // Looks like we found the word we were searching for
                // If it has not yet been added to our return string...
                if (!hasBeenAdded) {													/*4*/
                    // ...we add it and set our flag to true. When a word
                    // is found more than once on a page, this ensures that
                    // the page URL is only added once
                    returnValue += "\t" + lastURL + "\n";
                    hasBeenAdded = true;
                }
            }

            // Get the reference to the next object in the linked list
            l = l.next;
        }

        return returnValue;
    }

   
	public static boolean exists(HTMLlist l, String word) {
        while (l != null) {																/*1*/
            if (l.str.equalsIgnoreCase (word))											/*2*/
                return true;

            l = l.next;
        }
        
        return false;
    }

	
    public static HTMLlist readHtmlList (String filename) throws IOException {
        String name;
        HTMLlist start, current, tmp;

        // Open the file given as argument
        BufferedReader infile = new BufferedReader(new FileReader(filename));

        name = infile.readLine(); //Read the first line
        start = new HTMLlist (name, null);
        current = start;
        name = infile.readLine(); // Read the next line

        while (name != null) {    // Exit if there is none								/*1*/
            tmp = new HTMLlist(name,null);
            current.next = tmp;
            current = tmp;            // Update the linked list
            name = infile.readLine(); // Read the next line
        }

        infile.close(); // Close the file
        return start;
    }
    
    public static String readHtmlListtoString (HTMLlist l){
    	while (l != null) {																/*1*/
            System.out.println(l.str);
            l = l.next;
        }
    	
    	
    	return null;
    	
    	
    }
    
}
